<template>
  <div>
    <h1 align="center">员工模块01</h1>
    <!-- 查询 -->
    <el-form :inline="true" ref="queryForm" :model="query"  class="demo-form-inline">
      <el-form-item label="员工名称">
        <el-input v-model="query.name" placeholder="员工名称" />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery()">查询</el-button>
        <el-button icon="el-icon-refresh" size="mini"  @click="resetQuery()">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 添加对话框 -->
    <div>
      <el-button
        type="primary"
        plain
        icon="el-icon-plus"
        size="mini"
        @click="handleAdd()"
      >添加</el-button>
    </div>
    <el-dialog :visible.sync="dialogVisible" width="30%" :title="title">
      <el-form ref="department" :model="department" label-width="80px">
        <el-form-item label="员工名称">
          <el-input v-model="department.name" />
        </el-form-item>
        <el-form-item label="员工地址">
          <el-input v-model="department.address" />
        </el-form-item>
        <el-form-item label="员工手机">
          <el-input v-model="department.iphone" />
        </el-form-item>
        <el-form-item prop="personId" label="部门名称">
          <el-select v-model="department.personId" clearable placeholder="请输入部门名称">
            <el-option
              v-for="person in personList"
              :key="person.id"
              :label="person.name"
              :value="person.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <!--        <el-form-item label="性别">-->
        <!--          <el-select v-model="department.sex"   clearable placeholder="请选择性别">-->
        <!--            <el-option label="男" value= 0 />-->
        <!--            <el-option label="女" value= 1 />-->
        <!--            <el-option label="未知" value= 2 />-->
        <!--          </el-select>-->
        <!--        </el-form-item>-->
        <el-form-item label="性别">
          <el-select v-model="department.sex" clearable placeholder="请选择性别">
            <el-option
              v-for="it in sexArrays"
              :key="it.value"
              :label="it.label"
              :value="it.value"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancel()">取 消</el-button>
        <el-button type="primary" @click="save">保 存</el-button>
      </span>
    </el-dialog>

    <!-- 表格 -->
    <el-table :data="tableData" style="width: 100%">
      <el-table-column label="序号" align="center" width="180">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.id }}</span>
        </template>
      </el-table-column>

      <el-table-column label="员工名称" width="180" align="center">
        <template slot-scope="scope">
          <el-tag size="medium">{{ scope.row.name }}</el-tag>
        </template>
      </el-table-column>

      <el-table-column label="员工地址" width="180" align="center">
        <template slot-scope="scope">
          <el-tag size="medium">{{ scope.row.address }}</el-tag>
        </template>
      </el-table-column>

      <el-table-column label="员工手机" width="180" align="center">
        <template slot-scope="scope">
          <el-tag size="medium">{{ scope.row.iphone }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="typePerson" label="部门类型" width="180" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.typePerson == 1">技术类</span>
          <span v-if="scope.row.typePerson == 2">非技术类</span>
          <span v-if="scope.row.typePerson == 3">其他部门</span>
        </template>
      </el-table-column>

      <el-table-column label="部门名称" width="180" align="center">
        <template slot-scope="scope">
          <el-tag size="medium">{{ scope.row.personName }}</el-tag>
        </template>
      </el-table-column>

      <el-table-column prop="sex" label="性别" width="80">
        <template slot-scope="scope">
          <span v-if="scope.row.sex == 0">男</span>
          <span v-if="scope.row.sex == 1">女</span>
          <span v-if="scope.row.sex == 2">未知</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="handleEdit(scope.$index, scope.row)"
          >编辑</el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <div class="block">
      <el-pagination
        :current-page="query.page"
        :page-sizes="arr"
        :page-size="query.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="count"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script>
import {daptAdd, PageList, daptEdit, deletDept} from '@/api/dep1'
import {GetPersonList} from '@/api/person'
import axios from 'axios'
export default {
  name: 'Dep01',
  data() {
    return {
      title: '',
      personList: [],
      count: 100,
      arr: [5, 10, 15, 20],
      // 查询数据模型
      query: {
        name: '',
        page: 1,
        size: 5
      },
      // 添加的模型数据
      department: {},

      // 添加的
      dialogVisible: false,

      tableData: [],
      sexArrays: [
        { label: '男', value: 0 },
        { label: '女', value: 1 },
        { label: '未知', value: 2 }
      ]
    }
  },

  // vuez中生命周期钩子函数
  created() {
    this.getList()
    this.getDepartmentList()
  },
  methods: {
    /** 搜索按钮操作 */
    handleQuery() {
      this.query.page = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.query.name = ''
      this.handleQuery()
    },
    // 取消按钮
    cancel() {
      this.dialogVisible = false
    },
    // 编辑
    handleEdit(index, row) {
      this.title = '修改员工信息'
      const id = row.id
      axios
        .get('http://127.0.0.1:9090/department/' + id)
        .then((res) => {
          this.department = res.data.data
          console.log('======= this.department========', this.department)
        })
        .catch((error) => {
          console.error('error page01 deprtment', error)
        })
      // this.department = {...row} // 拿到回显数据
      this.dialogVisible = true
      console.log('+++++++２３４３４', JSON.stringify(row))
    },

    handleAdd() {
      this.title = '添加员工信息'
      // console.log("======= this.getDepartmentList()========", this.getDepartmentList())
      this.department = {} //  清空数据
      this.dialogVisible = true
    },
    save() {
      this.$refs['department'].validate(valid => {
        if (valid) {
          if (this.department.id) {
            daptEdit(this.department).then(res => {
              this.getList()
              this.dialogVisible = false
            })
          } else {
            daptAdd(this.department).then(res => {
              this.getList()
              console.log('==========执行到deptAdd?   ')
              this.dialogVisible = false
            })
          }
        }
      })
    },
    handleSizeChange(val) {
      this.query.size = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.query.page = val
      this.getList()
    },

    handleDelete(index, row) {
      deletDept(row.id).then((res) => {
        this.getList()
      })
    },
    // getList() {
    //   PageList(this.query).then((res) => {  // // 使用 this.query 获取查询参数
    //     this.tableData = res.data.rows;
    //     console.log("JSON====",JSON.stringify(res))
    //     this.count = res.data.totalCount;
    //     console.log("@========= ", this.tableData);
    //   }).catch((error) => {
    //     console.log("xxxxxerrorxxxxx",error);
    //   })
    // },
    getList() {
      axios
        .get('http://127.0.0.1:9090/department/page01', {
          params: {
            name: this.query.name,
            page: this.query.page,
            size: this.query.size
          }
        })
        .then((res) => {
          this.tableData = res.data.rows
          this.count = res.data.totalCount
          // console.log(res);
        })
        .catch((error) => {
          console.error('error page01 deprtment', error)
        })
    },
    /**
     *
     */
    // 查询PersonList
    getDepartmentList() {
      axios
        .get('http://127.0.0.1:9090/person/PersonList')
        .then((res) => {
          this.personList = res.data.data
          console.log('===============getDepartmentList==============', res)
        })
        .catch((error) => {
          console.error('error page01 deprtment', error)
        })
      // console.log("===============ssss==============")
      // GetPersonList().then((res) => {
      //     console.log("res========> ".res)
      //     this.personList = res.data.data
      //
      //   });
    }
  }
}
</script>

<style>
</style>
